import axios from 'axios'
import JSONBIG from 'json-bigint'

axios.defaults.transformResponse = [
    function (data) {
        const json = JSONBIG({
            storeAsString: true
        })
        return json.parse(data)
    }
]

export const getClusterStates = () => axios.get<any, any>('/es/_cluster/stats');
export const getIndices = () => axios.get<any, any>('/es/_cat/indices?format=json');
export const getSearch = (i: string, q: string) => axios.get<any, any>(`/es/${i}/_search`, q ? { params: { q } } : {});


export const b2b = (value: any) => {
    //  console.log(params.name)
    var gb = 1024 * 1024 * 1024
    var mb = 1024 * 1024
    var kb = 1024
    if (value > gb) {
        return Math.ceil(value / gb) + 'GB'
    }
    if (value > mb) {
        return Math.ceil(value / mb) + 'MB'
    }
    if (value > kb) {
        return Math.ceil(value / kb) + 'KB'
    }
    return value + 'B'
}